/**
* 登录模块，提供登录相关业务功能
* @Date:    2015-6-16
* @author:  Jackson wang
* @version: 1.0
* @since:   1.0
*/
app.addModule('department', function(context) {

	'use strict';

	var moduleCode='department',
	    formServ,
		ajaxServ,
		msgServ,
		moduleEl,
		formEl,
		private_method=0,
		formData=0,
		
		mainTab = '#centerTabs';
		grid='#department-dg';
		
		tabAndGrid = mainTab + ' ' + grid;
		
		
	private_method = {
		getButton:function(){
			var btn = [ {
					text : '保存',
					iconCls : 'icon-ok',
					handler : function() {						
						save();
					}
				}, {
					text : '取消',
					iconCls : 'icon-cancel',
					handler : function() {
						cancel();						
					}
				}]
				
			return btn;
		}
		


	};
	return {
		/**
		*
		* 初始化方法，进行模块内部的初始化工作		
		*/
		init:function(){
			ajaxServ = context.getService('ajax-serv');
			msgServ = context.getService('msg-serv');			
		},

		/**
		*
		* 销毁方法，清除遗留数据及方法绑定，释放内存		*
		*/
		destory:function(){
			formServ = null;
			moduleEl = null;
			formEl = null;
			formData = null;
			msgServ = null;
			grid = null;
			tabAndGrid = null;
		},
		
		
		
		/**
		 * 主页面导航栏按钮监听函数
		 */
		onclick:function(event, element, elementType){
			moduleEl = context.getElement();
			
			var delegateTarget = event.delegateTarget;
			
			var target = event.target;
			
			var url = $target.attr("targetUrl");
			
			event.delegateTarget;
			
			event.preventDefault();
		},
		
		/**
		 * 删除行
		 */
		delRows:function (){
		var node = $(tabAndGrid).treegrid('getSelected');
		if(node){
			parent.$.messager.confirm("提示","确定要删除记录吗?",function(r){  
			    if (r){  
					$.post("/sysman/department/delDepartment.action", {id:node.organizationId}, function(rsp) {
						if(rsp.status){
							$dg.treegrid('remove', node.organizationId);
						}
						parent.$.messager.show({
							title : rsp.title,
							msg : rsp.message,
							timeout : 1000 * 2
						});
					}, "JSON").error(function() {
						parent.$.messager.show({
							title :"提示",
							msg :"提交错误了！",
							timeout : 1000 * 2
						});
					});
			    }  
			});
		}else{
			parent.$.messager.show({
				title :"提示",
				msg :"请选择一行记录!",
				timeout : 1000 * 2
			});
		}
	},
	//弹窗修改
	updRowsOpenDlg:function() {
		var row = $(tabAndGrid).treegrid('getSelected');
		if (row) {
			$('body').modalDialog({
				title : "编辑组织",
				width : 600,
				height : 400,
				href : "/sysman/department/departmentEditDlg.html",
				onLoad:function(){
					var f = $('body').find("#form");
					f.form("load", row);
				},			
				toolbar:this.private_method.getButton()
				
			});
		}else{
			parent.$.messager.show({
				title :"提示",
				msg :"请选择一行记录!",
				timeout : 1000 * 2
			});
		}
	},
	//弹窗增加
	addRowsOpenDlg:function () {
		var row = $dg.treegrid('getSelected');
		parent.$.modalDialog({
			title : "添加组织",
			width : 600,
			height : 400,
			href : "/sysman/department/departmentEditDlg.html",
			onLoad:function(){
				if(row){
					var f = $('body').modalDialog.find("#form");
					f.form("load", {"id":row.id});
				}
			},
			buttons : [ {
				text : '保存',
				iconCls : 'icon-ok',
				handler : function() {
					parent.$.modalDialog.openner= $grid;//因为添加成功之后，需要刷新这个treegrid，所以先预定义好
					var f = parent.$.modalDialog.handler.find("#form");
					f.submit();
				}
			}, {
				text : '取消',
				iconCls : 'icon-cancel',
				handler : function() {
					parent.$.modalDialog.handler.dialog('destroy');
					parent.$.modalDialog.handler = undefined;
				}
			}
			]
		});
	}
	
		

	};

});